package com.example.administrator.fragarmentdemotest_a.fragarment_demo.frag;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.fragarmentdemotest_a.R;
import com.example.administrator.fragarmentdemotest_a.RestScroll.DepthPageTransformer;
import com.example.administrator.fragarmentdemotest_a.RestScroll.RestScroller;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.adapter.PagerContactAdapter;
import com.example.administrator.fragarmentdemotest_a.utils.app.BaseFragment;

import java.lang.reflect.Field;

public class MainFragment_B extends BaseFragment {
    private ImageView iv_head;
    private TextView tv_add;
    private ViewPager vp;
    private TabLayout tbl;

    private BaseFragment[] frags;
    private String tbtxt[];

    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        View view = inflater.inflate(R.layout.frag_b, container, false);
        vp = view.findViewById(R.id.vp);
        tbl = view.findViewById(R.id.tbl);
        return view;
    }

    @Override
    protected void initData() {
        tbtxt = new String[]{"好友", "群聊", "通讯录", "联系人", "公众号"};
        frags = new BaseFragment[]{new ContactFriendFragment(), new ContactGroupFragment(),
                new ContactDeviceFragment(), new ContactBookFragment(), new ContactPublicFragment()};

        PagerContactAdapter adapter = new PagerContactAdapter(getChildFragmentManager(), frags, tbtxt);
        vp.setAdapter(adapter);

        vp.setOffscreenPageLimit(frags.length - 1);

        tbl.setTabTextColors(Color.GRAY, Color.WHITE);
        tbl.setSelectedTabIndicatorHeight(20);
        tbl.setSelectedTabIndicatorColor(Color.WHITE);
        tbl.setTabMode(TabLayout.MODE_SCROLLABLE);
        tbl.setupWithViewPager(vp, true);
        tbl.setBackgroundColor(Color.DKGRAY);

//////////控制切换速度
        RestScroller mScroller = null;
        Field mField = null;
        try {
            mField = ViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            mScroller = new RestScroller(vp.getContext());
            mField.set(vp, mScroller);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //控制速度 秒数*1000=实际切换秒数
        mScroller.setmDuration(3 * 1000);


    }

    @Override
    protected void initListener() {
        //透明切换
        vp.setPageTransformer(true, new DepthPageTransformer());
        ////////////////////////////
        //3D切换//内凹切换
//        vp.setPageTransformer(true, new ViewPager.PageTransformer() {
//            @Override
//            public void transformPage(@NonNull View page, float position) {
//                sink3D(page, position);
//            }
//        });
    }


    ///盒子3D
    public void raised3D(View view, float position) {
        if (position >= -1&& position <= 1) {
            view.setPivotX(position < 0 ? view.getWidth() : 0);//设置要旋转的Y轴的位置
            view.setRotationY(90 * position);//开始设置属性动画值
        }
    }


    //内凹陷

    //凹陷3效果
    public void sink3D(View view, float position) {
        if (position >= -0.5f && position <= 0.5f) {
            view.setPivotX(position < 0 ? view.getWidth() : 0);
            view.setRotationY(-1 * position);
        }
    }

}
